﻿using Covid.Client.Services;
using Covid.Shared.Dtos;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;

namespace Covid.Client.Pages
{
    public class EmployeeOverviewBase : ComponentBase
    {


        //v1
        ////注入HttpClient，如果能在DI容器找到就找到了，没找到就抛出异常
        //[Inject]
        //public HttpClient HttpClient { get; set; }


        //v2
        //注入HttpClient，如果能在DI容器找到就找到了，没找到就抛出异常
        [Inject]
        public IEmployeeService EmployeeService { get; set; }


        //3.注入 IJSRuntime
        [Inject]
        public IJSRuntime JSRuntime { get; set; }


        //写一个属性，用来做单项绑定
        public string Result { get; set; }

        /// <summary>
        /// 前端为空会抛出异常，需要赋初始值
        /// </summary>
        public IEnumerable<EmployeeDto> Employee { get; set; } = new List<EmployeeDto>();




        /// <summary>
        /// 生命周期方法 ，Component被初始化之后会调用这个方法
        /// </summary>
        /// <returns></returns>
        protected override async Task OnInitializedAsync()
        {

            //v1
            //Employee = await HttpClient.GetFromJsonAsync<IList<EmployeeDto>>("/api/department/1/employee");


            //v2
            Employee = await EmployeeService.GetForDepartmentAsync(2);
            await base.OnInitializedAsync();
        }



        //5.实现单击事件
        public async Task SayHello() {
            Result = await JSRuntime.InvokeAsync<string>("sayHello", "刘滔");
        }


    }
}
